package com.zy.imagineCommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.entity.CommentTextThree;
import com.zy.imagineCommunity.entity.Page;
import com.zy.imagineCommunity.entity.ReplyText;
import com.zy.imagineCommunity.entity.ReplyTextTwo;
import com.zy.imagineCommunity.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CommentManagementController {

    @Autowired
    CommentService commentService;

    /**
     * 主评论管理页面
     *
     * @return
     */
    @GetMapping("/toCommentManagement")
    public String getCommentManagement() {
        return "/commentManagement";
    }

    /**
     * 主评论管理
     *
     * @param page
     * @return
     */
    @ResponseBody
    @PostMapping("/commentManagement")
    public JSONObject commentManagement(int page) {
        JSONObject jsonObject = new JSONObject();

        //通过id查询用户
        Page<CommentTextThree> pageText = commentService.queryCommentsByPageNoAndPageSize(page, 10);

        for (CommentTextThree commentTextThree : pageText.getItems()) {
            System.out.println(commentTextThree);
        }

        jsonObject.put("code", 0);
        jsonObject.put("count", pageText.getPageCountTotal());
        jsonObject.put("data", pageText.getItems());

        return jsonObject;
    }

    /**
     * 主回复管理页面
     *
     * @return
     */
    @GetMapping("/toReplyManagement")
    public String getReplyManagement() {
        return "/replyManagement";
    }

    @ResponseBody
    @PostMapping("/replyManagement")
    public JSONObject replyManagement(int page) {
        JSONObject jsonObject = new JSONObject();

        //通过id查询用户
        Page<ReplyTextTwo> pageText = commentService.queryReplyByPageNoAndPageSize(page, 10);

        for (ReplyTextTwo replyTextTwo : pageText.getItems()) {
            System.out.println(replyTextTwo);
        }

        jsonObject.put("code", 0);
        jsonObject.put("count", pageText.getPageCountTotal());
        jsonObject.put("data", pageText.getItems());

        return jsonObject;
    }
//

    /**
     * 分类评论管理页面
     *
     * @return
     */
    @GetMapping("/toCommentManagementByType/{type}")
    public String getCommentManagementByType(@PathVariable("type") int type, Model model) {
        model.addAttribute("type", type);
        return "/commentManagementByType";
    }

    /**
     * 分类评论管理
     *
     * @param page
     * @return
     */
    @ResponseBody
    @PostMapping("/commentManagementByType/{type}")
    public JSONObject commentManagementByType(@PathVariable("type") int type, int page) {
        JSONObject jsonObject = new JSONObject();

        //通过id查询用户
        Page<CommentTextThree> pageText = commentService.queryCommentsByTypeAndPageNoAndPageSize(type, page, 10);

        for (CommentTextThree commentTextThree : pageText.getItems()) {
            System.out.println(commentTextThree);
        }

        jsonObject.put("code", 0);
        jsonObject.put("count", pageText.getPageCountTotal());
        jsonObject.put("data", pageText.getItems());

        return jsonObject;
    }

    /**
     * 分类回复管理页面
     *
     * @return
     */
    @GetMapping("/toReplyManagementByType/{type}")
    public String getReplyManagementByType(@PathVariable("type") int type, Model model) {
        model.addAttribute("type", type);
        return "/replyManagementByType";
    }

    /**
     * 分类回复管理
     *
     * @param page
     * @return
     */
    @ResponseBody
    @PostMapping("/replyManagementByType/{type}")
    public JSONObject replyManagementByType(@PathVariable("type") int type, int page) {
        JSONObject jsonObject = new JSONObject();

        //通过id查询用户
        Page<ReplyTextTwo> pageText = commentService.queryReplyTextByTypeAndPageNoAndPageSize(type, page, 10);

        for (ReplyTextTwo replyTextTwo : pageText.getItems()) {
            System.out.println(replyTextTwo);
        }

        jsonObject.put("code", 0);
        jsonObject.put("count", pageText.getPageCountTotal());
        jsonObject.put("data", pageText.getItems());

        return jsonObject;
    }

    /**
     * 批量删除评论
     *
     * @param idList
     */
    @ResponseBody
    @PostMapping("/deleteCommentList")
    public void deleteCommentList(String idList) {

        String[] id = idList.split(",");

        for (int i = 0; i < id.length; i++) {
            if (!"".equals(id[i])) {
                int idInt = Integer.parseInt(id[i]);
                //删除评论以及下面的回复
                commentService.deleteCommentByCommentId(idInt);
                commentService.deleteReplyByCommentId(idInt);
            }
        }

    }

    /**
     * 批量删除回复
     *
     * @param idList
     */
    @ResponseBody
    @PostMapping("/deleteReplyList")
    public void deleteReplyList(String idList) {

        String[] id = idList.split(",");

        for (int i = 0; i < id.length; i++) {
            if (!"".equals(id[i])) {
                int idInt = Integer.parseInt(id[i]);
                //删除回复
                commentService.deleteReplyById(idInt);
            }
        }

    }

}
